import request from '@/utils/request';

/**
 * 获取月度销售数据
 * @param {Object} params 查询参数
 * @returns {Promise} Promise对象
 */
export function getMonthlySales(params) {
  return request({
    url: '/api/omm/orders/sales/monthly/',
    method: 'get',
    params
  });
}

/**
 * 获取产品销售占比
 * @param {Object} params 查询参数
 * @returns {Promise} Promise对象
 */
export function getProductSalesRatio(params) {
  return request({
    url: '/api/omm/orders/sales/product-ratio/',
    method: 'get',
    params
  });
}

/**
 * 获取销售概览数据
 * @returns {Promise} Promise对象
 */
export function getSalesOverview() {
  return request({
    url: '/api/omm/orders/sales/overview/',
    method: 'get'
  });
}

/**
 * 获取销售趋势数据
 * @param {Object} params 查询参数
 * @returns {Promise} Promise对象
 */
export function getSalesTrend(params) {
  return request({
    url: '/api/omm/orders/sales/trend/',
    method: 'get',
    params
  });
}

/**
 * 获取年度收入数据
 * @returns {Promise} Promise对象
 */
export function getYearlyRevenue() {
  return request({
    url: '/api/omm/orders/sales/yearly-revenue/',
    method: 'get'
  });
}

/**
 * 获取模拟销售数据（前端模拟，实际项目中应使用后端接口）
 * @param {Object} params 查询参数
 * @returns {Promise} 模拟数据的Promise
 */
export function getMockSalesData(params) {
  // 模拟月度销售数据
  const getMonthlySalesData = (year) => {
    const currentYear = year || new Date().getFullYear();
    const monthData = [];
    const totalSales = [];
    const orderCounts = [];
    
    for (let i = 0; i < 12; i++) {
      const monthName = new Date(currentYear, i, 1).toLocaleString('default', { month: 'short' });
      monthData.push(monthName);
      
      // 生成随机销售额，100000到500000之间
      const sales = Math.floor(Math.random() * 400000) + 100000;
      totalSales.push(sales);
      
      // 生成随机订单数，10到50之间
      const orders = Math.floor(Math.random() * 40) + 10;
      orderCounts.push(orders);
    }
    
    return {
      months: monthData,
      sales: totalSales,
      orders: orderCounts,
      year: currentYear
    };
  };
  
  // 模拟产品销售占比数据
  const getProductRatioData = () => {
    const products = ['ERP系统', '进销存系统', 'HR管理系统', 'CRM系统', '财务系统', '其他产品'];
    const salesData = [];
    
    // 生成随机销售额
    products.forEach(product => {
      salesData.push({
        name: product,
        value: Math.floor(Math.random() * 100) + 20
      });
    });
    
    return salesData;
  };
  
  // 模拟销售概览数据
  const getSalesOverviewData = () => {
    const currentMonth = new Date().getMonth() + 1;
    const currentYear = new Date().getFullYear();
    
    return {
      totalSales: Math.floor(Math.random() * 5000000) + 1000000, // 总销售额
      monthSales: Math.floor(Math.random() * 500000) + 100000, // 当月销售额
      orderCount: Math.floor(Math.random() * 500) + 100, // 总订单数
      monthOrderCount: Math.floor(Math.random() * 50) + 10, // 当月订单数
      activeCustomers: Math.floor(Math.random() * 300) + 50, // 活跃客户数
      currentMonth,
      currentYear
    };
  };
  
  // 模拟销售趋势数据
  const getSalesTrendData = () => {
    const days = [];
    const salesData = [];
    
    // 获取当月天数
    const currentDate = new Date();
    const year = currentDate.getFullYear();
    const month = currentDate.getMonth();
    const daysInMonth = new Date(year, month + 1, 0).getDate();
    
    for (let i = 1; i <= daysInMonth; i++) {
      days.push(i);
      // 生成随机日销售额
      const daySales = Math.floor(Math.random() * 50000) + 10000;
      salesData.push(daySales);
    }
    
    return {
      days,
      sales: salesData,
      month: month + 1,
      year
    };
  };
  
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        data: {
          monthlySales: getMonthlySalesData(params?.year),
          productRatio: getProductRatioData(),
          salesOverview: getSalesOverviewData(),
          salesTrend: getSalesTrendData()
        },
        message: '获取数据成功'
      });
    }, 500);
  });
} 